package com.wx.controller;

import com.alibaba.fastjson.JSONObject;
import com.wx.bean.Userbean;
import com.wx.service.impl.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/user")
public class userController {
    @Autowired
    IUserService iUserService;

    @GetMapping("/findAll")
    public JSONObject findAll() {
        List<Userbean> list= iUserService.findAll();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", list);
        return jsonObject;
    }

    @GetMapping("/username")
    public JSONObject username(HttpSession session) {
        String username= (String) session.getAttribute("username");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("username", username);
        return jsonObject;
    }

    @RequestMapping("/findById")
    public JSONObject findById(@RequestBody Userbean userbean) {
        Userbean userbean1 = iUserService.findById(userbean);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("userbean", userbean1);
        return jsonObject;
    }

    @RequestMapping("/findCondition")
    public JSONObject findCondition(@RequestBody Userbean userbean) {
        List<Userbean> list= iUserService.findCondition(userbean);
        int number= iUserService.getCount(userbean);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", list);
        jsonObject.put("number", number);
        return jsonObject;
    }


    @GetMapping("/findUserType")
    public JSONObject findUserType() {
        List<Userbean> list= iUserService.findUserType();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", list);
        return jsonObject;
    }

    @RequestMapping("/update")
    public JSONObject update(@RequestBody Userbean userbean) {
        int result = iUserService.update(userbean);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result", result);
        return jsonObject;
    }

    @RequestMapping("/userAdd")
    public JSONObject userAdd(@RequestBody Userbean userbean) {
        int result = iUserService.userAdd(userbean);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result", result);
        return jsonObject;
    }

    @RequestMapping("/delete")
    public JSONObject delete(@RequestBody Userbean userbean) {
        int result = iUserService.delete(userbean);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result", result);
        return jsonObject;
    }
}
